package com.dmtt.web.controller;

import com.dmtt.util.R;
import com.dmtt.web.entity.*;
import com.dmtt.web.service.CarManagerService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carManager")
public class CarManagerController {
    @Autowired
    private CarManagerService carManagerService;

    //添加Agv
    @RequestMapping(value = "/saveAgv")
    public R saveAgv(@RequestBody AgvInfo agv){
        List<AgvInfo> agvInfo1 = carManagerService.findAgvInfo1();
        for (AgvInfo a :agvInfo1){
            if (a.getId().equals(agv.getId())){
                return R.error("id重复");
            }
            if (a.getAgvName().equals(agv.getAgvName())){
                return R.error("agvName重复");
            }
        }
        carManagerService.saveAgv(agv);
        PageInfo<AgvInfo> agvInfo = carManagerService.findAgvInfo(1);
        return R.ok().put("a",agvInfo);
    }
    //删除Agv
    @RequestMapping(value = "/deleteAgv")
    public R deleteAgv(@RequestBody Agv agv){
        carManagerService.deleteAgv(agv);
        PageInfo<AgvInfo> agvInfo = carManagerService.findAgvInfo(1);
        return R.ok().put("a",agvInfo);

    }

    //查询AGC车辆信息
    @RequestMapping("/agcInfo")
    public R agcInfo(@RequestBody ActivationVo activationVo){
        PageInfo<AgvInfo> agvInfo = carManagerService.findAgvInfo(activationVo.getCurrPage());
        return R.ok().put("a",agvInfo);
    }
    //修改AGC车辆信息
    @RequestMapping("/updateAgcInfo")
    public R updateAgcInfo(@RequestBody AgvInfo agvInfo){
        if (agvInfo == null || StringUtils.isEmpty(agvInfo.getAgvName())|| agvInfo.getGroupName() == null){
            return R.ok().put("info", "字段为空");
        }
        carManagerService.updateAgcInfo(agvInfo);
        PageInfo<AgvInfo> agv = carManagerService.findAgvInfo(agvInfo.getCurrPage());
        return R.ok().put("info",agv);
    }

    //查询稼动率信息
    @RequestMapping("/querymovement")
    public R querymovement(@RequestBody ActivationVo activationVo){
        PageInfo<FaultInfo> querymovement = carManagerService.querymovement(activationVo.getCurrPage());
        return R.ok().put("info",querymovement);
    }

    //修改稼动率信息
    @RequestMapping("/updatemovement")
    public R updatemovement(@RequestBody FaultInfo faultInfo){
        carManagerService.updatemovement(faultInfo);
        PageInfo<FaultInfo> querymovement = carManagerService.querymovement(faultInfo.getCurrPage());
        return R.ok().put("info",querymovement);
    }
    //查询上下货站点原点信息
    @RequestMapping("/queryStation")
    public R queryStation(@RequestBody ActivationVo activationVo){
        PageInfo<StationUpdate> stationUpdates = carManagerService.queryStation(activationVo.getCurrPage());
        return R.ok().put("info",stationUpdates);
    }

    //更新上下货站点原点信息
    @RequestMapping("/updateStation")
    public R updateStation(@RequestBody StationUpdate stationUpdate){
        carManagerService.updateStation(stationUpdate);
        PageInfo<StationUpdate> stationUpdates = carManagerService.queryStation(stationUpdate.getCurrPage());
        return R.ok().put("info",stationUpdates);
    }

    //查询jobConfig的name
    @RequestMapping("/jobConfig")
    public R jobConfig(){
        List<StationUpdate> jobConfig = carManagerService.findJobConfig();
        return R.ok().put("info",jobConfig);
    }
    //添加上下货的站点原点信息
    @RequestMapping("/saveStation")
    public R saveStation(@RequestBody StationUpdate stationUpdate){
        return carManagerService.saveStation(stationUpdate);
    }
    //删除上下货的站点原点信息
    @RequestMapping("/deleteStation")
    public R deleteStation(@RequestBody StationUpdate stationUpdate){
        carManagerService.deleteStation(stationUpdate);
        PageInfo<StationUpdate> stationUpdates = carManagerService.queryStation(1);
        return R.ok().put("info",stationUpdates);
    }

    //去保养
    @RequestMapping("/warnCarUpdate")
    public R warnCarUpdate(@RequestBody AgvInfo agvInfo){
        return carManagerService.warnCarUpdate(agvInfo);
    }

    //坐标管理模块：查询站点和坐标，
    @RequestMapping("/queryPointXY")
    public R queryPoint(@RequestBody PointMap pointMap){
        PageInfo<PointMap> pointMaps = carManagerService.queryPointXY(pointMap);
        return R.ok().put("info",pointMaps);
    }
    //坐标管理模块：添加站点和坐标，
    @RequestMapping("/savePointXY")
    public R savePoint(@RequestBody PointMap pointMap){
        pointMap.setCurrPage(1);
        PageInfo<PointMap> pointMaps = carManagerService.queryPointXY(pointMap);
        if (pointMaps.getSize()==0){
            carManagerService.savePointXY(pointMap);
            PointMap pointMap1 = new PointMap();
            pointMap1.setCurrPage(1);
            return R.ok().put("info",carManagerService.queryPointXY(pointMap1));
        }
       return R.error("站点重复");
    }
    //坐标管理模块：修改站点和坐标，
    @RequestMapping("/updatePointXY")
    public R updatePoint(@RequestBody PointMap point){
        carManagerService.updatePointXY(point);
        PointMap pointMap1 = new PointMap();
        pointMap1.setCurrPage(point.getCurrPage());
        return R.ok().put("info",carManagerService.queryPointXY(pointMap1));
    }
    //坐标管理模块：删除站点和坐标，
    @RequestMapping("/deletePointXY")
    public R deletePoint(@RequestBody PointMap point){
        carManagerService.deletePointXY(point);
        PointMap pointMap1 = new PointMap();
        pointMap1.setCurrPage(1);
        return R.ok().put("info",carManagerService.queryPointXY(pointMap1));
    }

    //报警关闭状态
    @RequestMapping("/callPoliceUpdate")
    public R callPoliceUpdate(){
        return carManagerService.callPoliceUpdate();
    }

    //小车行驶中的实时访问位置
    @RequestMapping("/queryCar")
    public R queryCar(){
        return carManagerService.queryCar();
    }
}
